<template>
  <el-row type="flex" justify="space-between" align="middle" style="height: 50px,;width: 100%">
    <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
    <el-col>
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col> {{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown @command="handleCommand">
            <span>
              操作<i class="el-icon-arrow-down" />
            </span>
            <!-- 下拉菜单 -->
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">修改子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="delete">删除子部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  name: 'TreeTools',
  props: {
    treeNode: {
      type: Object,
      required: true

    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // 新增 编辑 删除操作
    handleCommand(type) {
      if (type === 'add') {
        this.$emit('adddepts', this.treeNode)
      } else if (type === 'edit') {
        this.$emit('editdepts', this.treeNode)
      } else {
        this.$confirm(`确定要删除【${this.treeNode.name}】部门吗`).then(() => {
          // 如果点击了确定就会进入then
          return delDepartment(this.treeNode.id) // 返回promise对象
        }).then(() => {
          //  如果删除成功了 就会进入这里 通知父组件更新
          this.$emit('delDept')
          this.$message.success('删除部门成功')
        })
      }
    }
  }
}
</script>

<style>

</style>
